// merchants/holderList/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    sendList: [],
    listType: 1, //1股东列表，2发放量，3核销量, 4联盟发放量
    coupontypeid: 0, //优惠券类型id
    checkList: [],
    allianceid: 0,
    createUid: 0
  },

  // 优惠券发放记录
  geSendList: function (coupontypeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/coupon/GetCouponUserList?createUid=' + app.globalData.userInfo.Uid + '&couponTypeId=' + coupontypeid + '&page=' + that.data.CurrentPage,
      success: function(res) {
        if (res.data.Code == 0) {
          wx.setNavigationBarTitle({
            title: '发放量（' + res.data.Data.PageModel.TotalCount + ')',
          })
          let sendList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.sendList
            sendList = preGoods.concat(sendList);
          }
          if (sendList.length > 0){
            for (let i = 0; i < sendList.length; i++){
              sendList[i].CreateTime = sendList[i].activatetime.substr(0, 10);
            }
          }
          that.setData({
            sendList: sendList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 优惠券核销记录
  getCheckList: function (coupontypeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/shareholder/GetCheckList?createUid=' + app.globalData.userInfo.Uid + '&couponTypeId=' + coupontypeid,
      success: function(res) {
        if (res.data.Code == 0) {
          wx.setNavigationBarTitle({
            title: '核销量（' + res.data.Data.PageModel.TotalCount + ')',
          })
          that.setData({
            checkList: res.data.Data.List,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 跳转客户详情页（股东列表）
  toCusDetail: function(e) {
    let shareUid = e.currentTarget.dataset.shareuid;
    let clientid = this.getClientid(shareUid);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.listtype) {
      if (options.coupontypeid){
        this.setData({
          coupontypeid: options.coupontypeid
        })
      }
      if (options.listtype == 2) {
        this.geSendList(options.coupontypeid);//发放量
      }
      if (options.listtype == 3) {
        this.getCheckList(options.coupontypeid); //核销量
      }
      if (options.listtype == 4) {
        if (options.allianceid){
          this.getPackageRecardList(options.allianceid, options.createUid);//联盟卡包发放量
          this.setData({
            allianceid: options.allianceid,
            createUid: options.createUid
          })
        }
      }
      this.setData({
        listType: options.listtype
      })
    }
  },
  // 联盟卡券已发放数量
  getPackageRecardList: function (id, createUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/shareholder/GetPackageRecardList?creatUid=' + createUid + '&allianceId=' + id + '&page=' + that.data.CurrentPage,
      success: function (res) {
        if (res.data.Code == 0) {
          wx.setNavigationBarTitle({
            title: '发放量（' + res.data.Data.PageModel.TotalCount + ')',
          })
          let sendList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.sendList;
            sendList = preGoods.concat(sendList);
          }
          if (sendList.length > 0) {
            for (let i = 0; i < sendList.length; i++) {
              sendList[i].CreateTime = sendList[i].CreateTime.substr(0, 10);
            }
          }
          that.setData({
            sendList: sendList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },


  // 加载更多
  loadMore: function(e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      if (that.data.listType == 2) {
        that.geSendList(that.data.coupontypeid);
      }
      if (that.data.listType == 3) {
        that.getCheckList(that.data.coupontypeid);
      }
      if(that.data.listType == 4){
        that.getPackageRecardList(that.data.allianceid, that.data.createUid);
      }
    } else {
      return
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
})